﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GisGen
{
    /// <summary>
    /// Gps坐标点
    /// </summary>
    public class GpsPoint
    {
        private double longitude;   //经度
        private double latitude;    //纬度
        private string longitudeText;   //经度文本
        private string latitudeText;    //纬度文本

        public GpsPoint()
        {
            this.longitude = 0.00;
            this.latitude = 0.00;
            this.longitudeText = "";
            this.latitudeText = "";
        }

        /// <summary>
        /// 经度
        /// </summary>
        public double Longitude
        {
            get { return longitude; }
            set { longitude = value; }
        }

        /// <summary>
        /// 纬度
        /// </summary>
        public double Latitude
        {
            get { return latitude; }
            set { latitude = value; }
        }

        /// <summary>
        /// 经度文本
        /// </summary>
        public string LongitudeText
        {
            get { return longitudeText; }
            set
            {
                longitudeText = value;
                longitude = DegreesToDigital(value);
            }
        }

        /// <summary>
        /// 纬度文本
        /// </summary>
        public string LatitudeText
        {
            get { return latitudeText; }
            set
            {
                latitudeText = value;
                latitude = DegreesToDigital(value);
            }
        }

        /// <summary>
        /// 经度（角度格式）
        /// </summary>
        public string LongitudeDegrees
        {
            get
            {
                return DigitalToDegrees(longitude);
            }
        }

        /// <summary>
        /// 维度（角度格式）
        /// </summary>
        public string LatitudeDegrees
        {
            get
            {
                return DigitalToDegrees(latitude);
            }
        }

        /// <summary>
        /// 角度坐标转数字坐标
        /// </summary>
        /// <param name="degrees"></param>
        /// <returns></returns>
        public double DegreesToDigital(string degrees)
        {
         
            if (degrees.IndexOf('°') > 0)
            {
                degrees = longitudeText.Replace("°", ":").Replace("′", ":").Replace("″", "");
            }
            if (degrees.IndexOf(':') == -1)
            {
                return Convert.ToDouble(degrees);
            }

            double digital = 0.00;
            string[] parts = degrees.Split(':');
            double d = Convert.ToDouble(parts[0]);
            double f = Convert.ToDouble(parts[1]);
            double s = Convert.ToDouble(parts[2]);

            digital += d;
            digital += f / 60;
            digital += s / (60 * 60);

            return digital;
        }

        /// <summary>
        /// 数字坐标转角度坐标
        /// </summary>
        /// <param name="digital">数字坐标</param>
        /// <returns>角度坐标</returns>
        public string DigitalToDegrees(double digital)
        {
            const double num = 60;
            int degree = (int)digital;
            double tmp = (digital - degree) * num;
            int minute = (int)tmp;
            double second = (tmp - minute) * num;

            string degrees = degree + "°" + minute + "′" + second.ToString("F3") + "″";
            return degrees;

        }
    }
}
